package awk.optional;


import awk.model.Brand;
import awk.model.Car;
import awk.model.User;
import java.util.Optional;

/**
 * created with IDEA
 *
 * @author haifeng.wang
 * @since 2019-06-19-17:25
 */
public class OptionalTest {

    public static void main(String[] args){
        Brand fordBrand = Brand.builder().brandName("ford").country("US").build();
        Car fordCar = Car.builder().brand(Optional.ofNullable(fordBrand)).price(18000).color("blue").build();
        User user = User.builder().age(29).name("awk").salary(24000).car(fordCar).build();
        //方式一：
        if (null != user && null != user.getCar()){
            Car car = user.getCar();
            Optional<Brand> brand = car.getBrand();
            //如果存在，就执行
            brand.ifPresent(Brand::getBrandName);
        }

        Optional<Car> optionalCar = Optional.ofNullable(fordCar);
        User optionalUser = User.builder().age(29).name("awk").salary(24000).optionalCar(optionalCar).build();
        //
        Optional.ofNullable(optionalUser).flatMap(User::getOptionalCar).flatMap(Car::getBrand).map(Brand::getBrandName).orElse("unKnow");


    }
}
